//
//  SettingUpVC.m
//  echo
//
//  Created by dllo on 16/3/1.
//  Copyright © 2016年 Danny. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width / 414
#define HEIGHT [UIScreen mainScreen].bounds.size.height / 736

#import "SettingUpVC.h"
#import "CellForSettingVC.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <DKNightVersion.h>
#import "CollectVC.h"

@interface SettingUpVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)UIButton *buttonForBack;
@property (nonatomic, retain)UIImageView *imageViewForHeader;
@property (nonatomic, retain)UIView *viewForHeader;
@end

@implementation SettingUpVC

- (void)dealloc{
    [_buttonForBack release];
    [_imageViewForHeader release];
    [_viewForHeader release];
    self.tableView.delegate = nil;
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.imageViewForHeader = [[UIImageView alloc]init];
    self.imageViewForHeader.frame = CGRectMake(0, 0, self.view.frame.size.width, 240 * HEIGHT);
    [self.view addSubview:self.imageViewForHeader];
//    [self.imageViewForHeader sd_setImageWithURL:[NSURL URLWithString:@"http://cdn.duitang.com/uploads/item/201410/29/20141029111953_TisnA.thumb.700_0.jpeg"]];
    self.imageViewForHeader.image = [UIImage imageNamed:@"shezhi1"];
    
    UILabel *labelForMine = [[UILabel alloc]init];
    labelForMine.text = @"我的";
    labelForMine.dk_textColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor whiteColor]);
    labelForMine.frame = CGRectMake(self.view.frame.size.width / 2 - 20 * WIDTH, 15 * HEIGHT, 40 * WIDTH, 50 * HEIGHT);
    [self.view addSubview:labelForMine];
    
    self.buttonForBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:self.buttonForBack];
    self.buttonForBack.frame = CGRectMake(5 * WIDTH, 20 * HEIGHT, 35 * WIDTH, 35 * WIDTH);
    [self.buttonForBack setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-2"] forState:UIControlStateNormal];
    [self.buttonForBack addTarget:self action:@selector(handleBack) forControlEvents:UIControlEventTouchUpInside];

    [self createTableView];
}

- (void)handleBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createTableView{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 240 * HEIGHT, self.view.frame.size.width, self.view.frame.size.height - 240 * HEIGHT - 44 * HEIGHT) style:UITableViewStyleGrouped];
    
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView release];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.tableView registerClass:[CellForSettingVC class] forCellReuseIdentifier:@"poolForSetting"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        
        return 2;
    }else if (section == 1){
        
        return 1;
    }else{
        
        return 0;
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            CellForSettingVC *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForSetting"];
            cell.imageViewForLeft.image = [UIImage imageNamed:@"iconfont-shoucang"];
            cell.labelForContent.text = @"我的收藏";
            cell.labelForContent.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
            cell.accessoryType = 1;
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            return cell;
        }else{
            CellForSettingVC *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForSetting"];
            cell.imageViewForLeft.image = [UIImage imageNamed:@"iconfont-yejian"];
            cell.labelForContent.text = @"夜间模式";
            cell.labelForContent.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
            cell.swichForNightModel.hidden = NO;
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            return cell;
        }
        
        
    }else {
      
            CellForSettingVC *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForSetting"];
            cell.imageViewForLeft.image = [UIImage imageNamed:@"iconfont-qingchuhuancun"];
            cell.labelForContent.text = @"清理缓存";
            cell.labelForContent.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
            NSUInteger size = [[SDImageCache sharedImageCache] getSize];
            float cacheSize = size / 1024.0 / 1024.0;
            cell.labelForCache.hidden = NO;
            [cell.labelForCache setText:[NSString stringWithFormat:@"%.2fM", cacheSize]];
            cell.labelForCache.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            return cell;
        
    
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    self.viewForHeader = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 33 * HEIGHT)];
    self.viewForHeader.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    return self.viewForHeader;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50 * HEIGHT;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 33 * HEIGHT;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01 * HEIGHT;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            CollectVC *vc = [[CollectVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            // 缓存大小
            NSUInteger size = [[SDImageCache sharedImageCache] getSize];
            float cacheSize = size / 1024.0 / 1024.0;
            
            NSString *string = [NSString stringWithFormat:@"清除缓存:%.2fM",cacheSize];
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:string preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            
            UIAlertAction *OK = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                //清除缓存
                [[SDImageCache sharedImageCache] clearDisk];
                
                [self.tableView reloadData];
                
            }];
            
            [alert addAction:cancel];
            [alert addAction:OK];
            [self presentViewController:alert animated:YES completion:nil];
  
        }
    }
}

- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBarHidden = YES;
    
    self.tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    self.viewForHeader.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
